package com.hcy.userapi.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcy.rabbitbasicmodel.dto.MyPage;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicutils.utils.CheckUtils;
import com.hcy.userdomin.domain.entity.OperateLog;
import com.hcy.userdomin.domain.service.impl.OperateLogServiceImpl;
import com.hcy.usermodel.dto.OptLogDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/5/12
 **/
@RestController
@RequestMapping("/common")
@Slf4j
public class UserCommonController {

    @Autowired
    OperateLogServiceImpl operateLogService;

    /**
     * @Author huchenying
     * @Description 获取操作日志
     * @Date 2022/5/12 20:02
     **/
    @PostMapping("/getOptLogList")
    public ResponseData<MyPage<OptLogDTO>> getOptLogList(@Validated @RequestBody OptLogDTO request){
        ResponseData<MyPage<OptLogDTO>> responseData = new ResponseData<>();
        LambdaQueryWrapper<OperateLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(request.getUserName()),OperateLog::getUserName,request.getUserName());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(request.getOperateType()),OperateLog::getOperateType,request.getOperateType());
        queryWrapper.between(CheckUtils.checkVarIsNotNullOrEmpty(request.getCreateTimeStart()) && CheckUtils.checkVarIsNotNullOrEmpty(request.getCreateTimeEnd()) ,
                OperateLog::getCreateTime,request.getCreateTimeStart(),request.getCreateTimeEnd());
        queryWrapper.orderBy(true,false,OperateLog::getCreateTime);
        Page<OperateLog> page = operateLogService.page(new Page<>(request.getPageNO(), request.getPageSize()), queryWrapper);
        MyPage<OptLogDTO> res = new MyPage<>(page.getCurrent(), page.getSize(),page.getTotal());
        List<OptLogDTO> optLogDTOList = new ArrayList<>();
        for (OperateLog record : page.getRecords()) {
            OptLogDTO optLogDTO = new OptLogDTO();
            BeanUtils.copyProperties(record,optLogDTO);
            optLogDTOList.add(optLogDTO);
        }
        res.setRecords(optLogDTOList);

        responseData.success(res);
        return responseData;
    }
}
